package com.adam.example.aggregation.bucket;

import com.adam.example.TransportClientHolder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.children.Children;
import org.elasticsearch.search.aggregations.bucket.children.ChildrenAggregationBuilder;

import java.net.UnknownHostException;

public class ChildrenAggregationExample {

    public static void main(String[] args) throws UnknownHostException {
        TransportClient transportClient = TransportClientHolder.getInstance().getTransportClient();

        ChildrenAggregationBuilder childrenAggregationBuilder = AggregationBuilders.children("agg", "employee");
        SearchResponse searchResponse = transportClient.prepareSearch("test_company")
                        .setSize(0).addAggregation(childrenAggregationBuilder).get();
        Children children = searchResponse.getAggregations().get("agg");
        System.out.println(children.getDocCount());

        transportClient.close();
    }

}
